package net.godutch.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.godutch.client.GreetingService;
import net.godutch.client.entity.Group;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService
{

    public String greetServer(String input)
    {
        String serverInfo = getServletContext().getServerInfo();
        String userAgent = getThreadLocalRequest().getHeader("User-Agent");
        int count = 0;
        PersistenceManager pm = PersistenceManagerInstance.getInstance();
        Group group = new Group();
        group.setName("123");
        try
        {
            pm.makePersistent(group);
        }
        catch(Exception e)
        {
            return e.getMessage();
        }
        finally
        {
            pm.close();
        }
        
        pm = PersistenceManagerInstance.getInstance();
        try
        {
            Query query = pm.newQuery(Group.class);
            List<Group> results = (List<Group>) query.execute();
            count = results.size();
        }
        catch(Exception e)
        {
            return e.getMessage();
        }
        finally
        {
            pm.close();
        }
        return "Hello, " + input + "!<br>There are "+ count +"!<br>I am running " + serverInfo + ".<br><br>It looks like you are using:<br>" + userAgent;
    }
}
